package com.alameda.common.config.document;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * 文档配置类
 *
 * @author Lv
 * @date 2024/2/19
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
    /**
     * 服务名称
     */
    @Value("${spring.application.name}")
    private String serviceName;

    /**
     * 注入knife4j
     *
     * @return
     */
    @Bean(value = "defaultApi")
    public Docket defaultApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title(serviceName + " restful apis")
                        .description("# " + serviceName + "服务请求前缀:  /" + serviceName)
                        .termsOfServiceUrl("http://www.xxx.com/")
                        .version("1.0")
                        .build())
                //分组名称
                .groupName("1.0版本")
                .select()
                // 指定 @ApiOperation 标注的接口被加入文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

}